<?php

class ObjectFields
{
	
	public $allowedTypes = array('text', 'array', 'checkbox', 'textarea', 'objectList', 'file', 'custom', 'richtext', 'select', 'password');
	
	public static function GetTypes()
	{
		$obj = new ObjectFields;
		return $obj->allowedTypes;
	}
	
	public static function GetField($item, $data, $object = null)
	{	
		$item['data'] = $data;
		$item['object'] = $object;
		$fakeField = $item['name'] . '_VALUE';
		if(isset($object->$fakeField))
		{
			$item['value_value'] = $object->$fakeField;
		}
		
		if(!in_array($item['type'], self::getTypes()))
		{
			return;
		}
		
		switch($item['type'])
		{
			case 'text':
				$out = self::GetText($item);
			break;
			
			case 'checkbox':
				$out = self::GetCheckbox($item);
			break;
			
			case 'textarea':
				$out = self::GetTextarea($item);
			break;
			
			case 'array':
				$out = self::GetArrayFields($item, $data);
			break;
			
			case 'objectList':
				$out = self::GetObjectList($item);
			break;
			
			case 'file':
				$out = self::GetFile($item);
			break;
			
			case 'richtext':
				$out = self::GetRichtext($item);
			break;
			
			case 'select':
				$out = self::getSelectField($item);
			break;
			
			case 'password':
				$out = self::getPasswordField($item);
			break;
			
			case 'custom':
				$itemData = call_user_func(array($object, $item['method']));
				$out = self::GetField($itemData, $itemData['data'], $object);
			break;
			
		}
		
	
		
		if(!isset($out))
		{
			return false;
		}
		
		return $out;
		
	}
	
	
	public static function GetText($item)
	{
		global $smarty;
		$smarty->assign('field', $item);
		$out = $smarty->fetch(PATH . 'library/templates/fields/text.tpl');
		return $out;
	}

	public static function GetPasswordField($item)
	{
		global $smarty;
		$smarty->assign('field', $item);
		$out = $smarty->fetch(PATH . 'library/templates/fields/password.tpl');
		return $out;
	}
	
	public static function GetCheckbox($item)
	{
		global $smarty;
		$smarty->assign('field', $item);
		$out = $smarty->fetch(PATH . 'library/templates/fields/checkbox.tpl');
		return $out;
	}

	public static function GetTextarea($item)
	{
		global $smarty;
		$smarty->assign('field', $item);
		$out = $smarty->fetch(PATH . 'library/templates/fields/textarea.tpl');
		return $out;
	}
	
	public static function getSelectField($item)
	{
		global $smarty;
		$smarty->assign('field', $item);
		$out = $smarty->fetch(PATH . 'library/templates/fields/select.tpl');
		return $out;
	} 
	
	
	public static function getArrayFields($item, $data)
	{
		if(!isset($item['fields']) || !is_array($item['fields']))
		{
			return false;
		}
		
		
		$dataArray = array();
		if(is_array($data))
		{
			$dataArray = $data;
		}
		
		$outHtml = array();
		$x = 0;
		foreach($item['fields'] as $key=>$field)
		{
				
			$dataA = '';
			if(isset($dataArray[$key][$x]))
			{
				$dataA = $dataArray[$key][$x];
			}
			
			$field['name'] = $item['name'].'['.$key.'][]';
			$field['label'] = $key;
			$out = self::GetField($field, $dataA, $item['object']);
			//$x++;
		
			if($out!='')
			{			
				$oneField = array();
				$oneField['name'] = $key.'[]';
				$oneField['field'] = $out;
				$outHtml[] = $oneField;
			}	
	
		}
		
		return $outHtml;	
	}
	
	
	public static function GetObjectList($item)
	{
		global $smarty;
		$obj = new $item['object'];
		$params = array('sort' => $item['value']);
		$list = $obj->getList($params);
		$smarty->assign('oList', $list);
		$smarty->assign('field', $item);
		$out = $smarty->fetch(PATH . 'library/templates/fields/objectList.tpl');
		return $out;
	}
	
	public static function GetFile($item)
	{
		global $smarty;
		$smarty->assign('field', $item);
		$out = $smarty->fetch(PATH . 'library/templates/fields/file.tpl');
		return $out;
	}
	
	public static function GetRichtext($item)
	{
		//debug($GLOBALS);
		global $smarty;
		global $mceLoaded;
		
		$smarty->assign('mceLoaded', $mceLoaded);
		$smarty->assign('field', $item);
		
		$smarty->assign('path_tiny_mce', WWW . 'library/3rdpart/tiny_mce/tiny_mce.js');
		
		if(!$mceLoaded)
		{
			$mceLoaded = true;
		}
		
		$out = $smarty->fetch(PATH . 'library/templates/fields/richtext.tpl');
		return $out;
		
	}
	
	
	
	
	
	
	
	
	

}


?>